package light.mvc.service.voter.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.registry.infomodel.User;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.sys.Trole;
import light.mvc.model.sys.Tuser;
import light.mvc.model.sys.Tvoterinfo;
import light.mvc.model.voter.Tfinishvoter;
import light.mvc.model.voter.Tvoter;
import light.mvc.pageModel.base.Tree;
import light.mvc.pageModel.base.Voter;
import light.mvc.service.voter.VoterServiceI;
import light.mvc.utils.Highcharts;
import light.mvc.utils.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

@Service
public class VoterServiceImpl implements VoterServiceI{
	
	@Autowired
	private BaseDaoI<Tvoter> bdi;
	@Autowired
	private BaseDaoI<Trole> udi;
	@Autowired
	private BaseDaoI<Tvoterinfo> vdi;
	@Autowired
	private BaseDaoI<Tfinishvoter> fdi;
	@Autowired
	private BaseDaoI<Tuser> usdi;

	@Override
	public List<Tvoter> find(Tvoter t) {
		return bdi.find(StringUtil.getHql("Tvoter", t), StringUtil.beanToMap(t));
	}

	@Override
	public Tvoter get(Tvoter t) {
		return bdi.get(StringUtil.getHql("Tvoter", t), StringUtil.beanToMap(t));
	}

	@Override
	public void saveOrUpdate(Tvoter t) {
		if(t.getCreatedate()==null){
			t.setCreatedate(new Date());
		}
		bdi.saveOrUpdate(t);
		if(t.getId()!=null&&t.getVoter()!=null){
			String []vs = t.getVoter().split(",");
			for(String v:vs){
				Tvoterinfo vi = 
					new Tvoterinfo(t.getId(),Long.parseLong(v),"3",t.getCreatedate());
				vdi.saveOrUpdate(vi);
			}
		}
		
	}

	@Override
	public void delete(Tvoter t) {
		bdi.delete(t);
		
	}

	@Override
	public List<Tree> tree(Tuser t) {
		Set<Tuser> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		Trole o = udi.get("select distinct t from Trole t where t.id="+t.getUsertype());

		if (o != null) {
			l = o.getUsers();
			for (Tuser r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				tree.setText(r.getName());
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public List<Tvoter> find(Tvoterinfo t) {
		List<Tvoterinfo> list = 
			vdi.find(StringUtil.getHql("Tvoterinfo", t), StringUtil.beanToMap(t));
		List<Tvoter> li = new ArrayList<Tvoter>();
		Map<Long, Tvoter> map = new HashMap<Long, Tvoter>();
		for(Tvoterinfo vi:list){
			Tvoter v = bdi.get(Tvoter.class, vi.getVoter_id());
			if(v!=null&&v.getTitle()!=null){
				if(map.containsKey(v.getId()))
					continue;
				else 
					map.put(v.getId(), v);
				v.setCreatedate(vi.getCreatedate()!=null?vi.getCreatedate():new Date());
				v.setVi_id(vi.getId());
				li.add(v);
			}
		}
		return li;
	}

	@Override
	public void updateVoterInfo(String id, String voters) {
		Long vid = Long.parseLong(id);
		Tvoterinfo tv = vdi.get(Tvoterinfo.class, vid);
		tv.setResult("1");
		tv.setCreatedate(new Date());
		vdi.saveOrUpdate(tv);
		
		String vs[] = voters.split(",");
		for(String v:vs){
			Tfinishvoter tf = new Tfinishvoter(vid, Long.parseLong(v), new Date());
			fdi.saveOrUpdate(tf);
		}
		
	}

	@Override
	public Tfinishvoter getfh(Tfinishvoter tf) {
		return fdi.get(StringUtil.getHql("Tfinishvoter", tf),StringUtil.beanToMap(tf));
	}

	@Override
	public Map<String,Object> getColumn(Tvoter t) {
		t = bdi.get(Tvoter.class, t.getId());
		List<String> categories = new ArrayList<String>();
		List<Map<String,Object>> series = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String, Object>();
		List<Voter> lo = new ArrayList<Voter>();
		if(t!=null){
			String candidates[] = t.getCandidate().split(",");
			List<Integer> data = new ArrayList<Integer>();
			for(String c:candidates){
				Voter vv = new Voter();
				Tuser u = usdi.get(Tuser.class,Long.parseLong(c));
				categories.add(u.getName());
				vv.setName(u.getName());
				Tvoterinfo tv = new Tvoterinfo();
				tv.setVoter_id(t.getId());
				List<Tvoterinfo> ltv = vdi.find(StringUtil.getHql("Tvoterinfo", tv), StringUtil.beanToMap(tv));
				int i = 0;
				for(Tvoterinfo vt:ltv){
					Tfinishvoter tf = new Tfinishvoter();
					tf.setUser_id(u.getId());
					tf.setVi_id(vt.getId());
					List<Tfinishvoter> lf = fdi.find(StringUtil.getHql("Tfinishvoter", tf), StringUtil.beanToMap(tf));
					i+=lf.size();
				}
				data.add(i);
				vv.setVoters(i+"");
				vv.setLv(((i*1.0D)/candidates.length)*100+"%");
				lo.add(vv);
			}
			map.put("name", "投票");
			map.put("data", data);
			series.add(map);
		}
		String str = Highcharts.createColumn(t.getTitle(), JSON.toJSONString(categories), JSON.toJSONString(series));
		map.clear();
		map.put("str", str);
		map.put("lo", lo);
		return map;
	}

	@Override
	public String getPie(Tvoter t) {
		t = bdi.get(Tvoter.class, t.getId());
		List<Object> list = new ArrayList<Object>();
		Map<String, Object> m1 = new HashMap<String, Object>();
		Map<String, Object> m2 = new HashMap<String, Object>();
		if(t!=null){
			String candidates[] = t.getCandidate().split(",");
			Tvoterinfo tv = new Tvoterinfo();
			tv.setVoter_id(t.getId());
			tv.setResult("1");
			List<Tvoterinfo> ltv = vdi.find(StringUtil.getHql("Tvoterinfo", tv), StringUtil.beanToMap(tv));
			double lv = ltv.size()>0?((ltv.size()*1.0D)/candidates.length)*100:0.0;
			m1.put("y", lv);
			m2.put("y", 1-lv);
		}
		m1.put("name", "已投票");m1.put("sliced", true);m1.put("selected", true);
		m2.put("name", "未投票");
		list.add(m1);
		list.add(m2);
		return Highcharts.createPie("投票进度", "数目", list);
	}
}
